﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Tasks;
using MobileSales.Website.TaskService;
using MobileSales.Website.Infrastructure;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using MobileSales.Model.Exceptions;
using System.ServiceModel;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class TaskTypesController : ControllerBase
    {
        #region Index
        //
        // GET: /TaskTypes/

        public ActionResult Index()
        {
            IEnumerable<TaskType> types = null;
            try
            {
                TaskServiceClient taskService = new TaskServiceClient();
                taskService.AddValidationToken(this.Token);
                types = taskService.GetTaskTypeList();
                taskService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskTypeList, MessageType.Error);
            }
            return View(types);
        } 
        #endregion

        #region Create
        //
        // GET: /TaskTypes/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /TaskTypes/Create

        [HttpPost]
        public ActionResult Create(TaskType taskType)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreTaskType(taskType);
                client.Close();
                this.SetMessage(InfoStrings.TaskTypeAdded, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskTypeAdding, MessageType.Error);

            }
            return View(taskType);
        } 
        #endregion

        #region Edit
        //
        // GET: /TaskTypes/Edit/5

        public ActionResult Edit(int id)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                TaskType type = client.GetTaskType(id);
                client.Close();
                return View(type);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.TaskTypeNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /TaskTypes/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, TaskType taskType)
        {
            try
            {
                taskType.Id = id;
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreTaskType(taskType);
                client.Close();
                this.SetMessage(InfoStrings.TaskTypeUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskTypeUpdating, MessageType.Error);

            }
            return View(taskType);
        } 
        #endregion

        #region Delete
        //
        // GET: /TaskTypes/Delete/5

        public ActionResult Delete(int id)
        {
            return Edit(id);
        }

        //
        // POST: /TaskTypes/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.DeleteTaskType(id);
                client.Close();
                this.SetMessage(InfoStrings.TaskTypeDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskTypeDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
